﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AppLayer;
using AudioPlayerLib;
using WaveResources;
using SpotItServerAPI;
using SpotItComponents;


namespace SpotIt
{
    /// <summary>
    /// Interaction logic for SpotIt_MainWindow.xaml
    /// </summary>
    ///
    public partial class SpotIt_MainWindow : Window
    {
        Session session;
        Services services;
        AudioFiles theAudioFiles = AudioFiles.Instance;
        SpotIt.Properties.Settings settings = new SpotIt.Properties.Settings();
        
        public SpotIt_MainWindow(Session session, Services services)
        {
            InitializeComponent();
            this.session = session;
            this.services = services;
       }

        private void singlePlayerButton_Click(object sender, RoutedEventArgs e)
        {
            SpoIt_Single_Menu singleMenu = new SpoIt_Single_Menu(session, services);
            this.Hide();
            theAudioFiles.theFiles[2].Play();
            singleMenu.ShowDialog();
            
            if (singleMenu.Play)
            {
                SpotIt_Play_Single playForm = new SpotIt_Play_Single();
                playForm.startMusic += new SpotIt_Play_Single.voidMethod(playForm_startMusic);
                playForm.ShowDialog();
            }
            this.Show();
        }

        void playForm_startMusic()
        {
            settings.Reload();
            if (settings.musicOn)
            {
                theAudioFiles.theFiles[0].Play();
                if (main_noImage.Visibility == Visibility.Visible)
                {
                    main_noImage.Visibility = Visibility.Hidden;
                    main_musicImage.Opacity = 1;
                }
            }
            else if (main_noImage.Visibility == Visibility.Hidden)
            {
                main_musicImage.Opacity = .45;
                main_noImage.Visibility = Visibility.Visible;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            welcomeLabel.Content = "Welcome " + session.UserInfo.Username;


        }

        private void deckEditorButton_Click(object sender, RoutedEventArgs e)
        {
            AudioFiles theAudioFiles = AudioFiles.Instance;
            SpotIt_Symbol_Set_Manager setManager = new SpotIt_Symbol_Set_Manager(session, services);
            this.Hide();
            theAudioFiles.theFiles[2].Play();
            setManager.ShowDialog();
            this.Show();
            
        }

        private void Options_button_Click(object sender, RoutedEventArgs e)
        {
            SpotIt_SettingsWindow settingWindow = new SpotIt_SettingsWindow();
            theAudioFiles.theFiles[2].Play();
            settingWindow.ShowDialog();
            settings.Reload();
            if (!settings.musicOn)
            {
                main_noImage.Visibility = Visibility.Visible;
                main_musicImage.Opacity = .45;
            }
            else
            {
                if (!theAudioFiles.theFiles[0].IsPlaying)
                    theAudioFiles.theFiles[0].Play();
                main_noImage.Visibility = Visibility.Hidden;
                main_musicImage.Opacity = 1;
            }
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void MusicToggle_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[3].Play();

            if (settings.musicOn)
            {
                if (theAudioFiles.theFiles[0].IsPlaying)
                    theAudioFiles.theFiles[0].Stop();
                main_noImage.Visibility = Visibility.Visible;
                main_musicImage.Opacity = .45;
                settings.musicOn = false;
            }
            else
            {
                settings.musicOn = true;
                theAudioFiles.theFiles[0].Play();
                theAudioFiles.theFiles[0].changeVolume(settings.musicVolume);
                main_noImage.Visibility = Visibility.Hidden;
                main_musicImage.Opacity = 1;
            }
            settings.Save();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            if (main_noImage.Visibility == Visibility.Hidden && settings.musicOn)
                theAudioFiles.theFiles[0].Play();
            if (!settings.musicOn)
            {
                main_noImage.Visibility = Visibility.Visible;
                main_musicImage.Opacity = .45;
            }
        }

        private void multiplayerButton_Click(object sender, RoutedEventArgs e)
        {
            Spotit_Multi_lobby multiLobby = new Spotit_Multi_lobby(session, services);
            this.Hide();
            
            theAudioFiles.theFiles[2].Play();
            multiLobby.ShowDialog();
            this.Show();
            
        }

        

        

        

    }
}
